//
//  HESoundHUD.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/24.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HESoundHUD.h"
#import <AudioToolbox/AudioToolbox.h>

@interface HESoundHUD ()
@property (nonatomic, strong) MBProgressHUD *HUD;

@property (nonatomic, strong) UIImageView *mcpView;
@property (nonatomic, strong) UIImageView *animationView;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation HESoundHUD

#pragma mark - LifeCycle
+(instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static HESoundHUD *instance;
    dispatch_once(&onceToken, ^{
        instance = [[HESoundHUD alloc] init];
    });
    return instance;
}
 
- (UIWindow *)keyWindow {
    UIWindow *foundWindow = nil;
    NSArray  *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow  *window in windows) {
        if (window.isKeyWindow) {
            foundWindow = window;
            break;
        }
    }
    return foundWindow;
}

- (UIView *)getTopWindow {
    // 可能出现hub不出现问题，使用keywindow 被键盘遮挡
    UIView *warp = [self keyWindow];
    // 所以如下处理
    for(UIView*window in [UIApplication sharedApplication].windows)
    {
        if([window isKindOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")])
        {
            warp = [UIApplication sharedApplication].windows.lastObject;
        }
    }
    return warp;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;;
}

- (void)initUI {
    
    UIView *warp = [[UIView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, 120, 120)];
    warp.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];
    [self addSubview:warp];
    
    warp.layer.masksToBounds = YES;
    warp.layer.cornerRadius = 12;

    
    // 话筒
    _mcpView = [[UIImageView alloc] initWithFrame: CGRectMake(10, 50, 37, 70)];
    _mcpView.contentMode = UIViewContentModeScaleAspectFill;
    _mcpView.image = IMAGE_NAMED(@"toast_microphone");
    [warp addSubview: _mcpView];
    
    // 节奏
    _animationView = [[UIImageView alloc] initWithFrame: CGRectMake(60, 50, 29, 64)];
    [self levelTimerCallback: 67];
    [warp addSubview:_animationView];
    
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.width.mas_offset(120);
        make.height.mas_offset(120);
    }];
 
    [_mcpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(warp).mas_offset(26);
        make.centerY.equalTo(warp);
        make.width.mas_offset(25);
        make.height.mas_offset(50);
    }];
    
    [_animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(_mcpView.mas_right).mas_offset(15);
        make.width.mas_offset(29);
        make.height.mas_offset(54);
    }];
}

- (void)show {
    
    self.frame = CGRectMake(0, 0, KScreenWidth, kScreenHeight);
    self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.2];
    [[self getTopWindow] addSubview:self];
  
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(repeat) userInfo:nil repeats:true];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
    self.alpha = 0;
    AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, ^{
            //播放震动完事调用的块
        });
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;

    } completion:^(BOOL finished) {
     
    }];
}

- (void)hide {
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    
    self.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;

    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)repeat {
    int y = 10 +  (arc4random() % 71); // [0 , 70]
    [self levelTimerCallback: y];
}

- (void)levelTimerCallback:(CGFloat)ff { 
    if (ff>0&&ff<=10) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_0")];
    } else if (ff>10 && ff<20) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_1")];
    } else if (ff >=20 &&ff<30) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_2")];
    } else if (ff >=30 &&ff<40) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_3")];
    } else if (ff >=40 &&ff<50) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_4")];
    } else if (ff >= 50 && ff < 60) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_5")];
    } else if (ff >= 60 && ff < 70) {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_6")];
    } else {
        [_animationView setImage:IMAGE_NAMED(@"toast_vol_7")];
    }
}

@end
